package com.x.gzcrm.web.controller;

import com.x.gzcrm.model.*;
import com.x.gzcrm.service.IStuInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StuInfoController {

    Logger logger = LoggerFactory.getLogger(CourseOrderController.class);

    @Autowired
    private IStuInfoService iStuInfoService;

    @RequestMapping("/list")
    public String list()
    {
        return "stuinfo/list";
    }

    @RequestMapping("listJson")
    @ResponseBody
    /**
     * page 显示当前页数
     * limit 每次显示多少条
     */
    public PageResult<StuInfo> listJson(StuInfo condition, int page, int limit)
    {

        //请求参数记录
        logger.info("=====请求参数=====");
        logger.info("condition："+condition);
        logger.info("page："+page);
        logger.info("limit："+limit);


        PageResult<StuInfo> result=iStuInfoService.findPageResult(condition,page,limit);//limit就是pagesize


        //响应数据记录
        logger.info("=====响应数据=====");
        logger.info("result："+result.toString());


        return  result;



    }


    //学生增加
    @RequestMapping("add")
    public String add(Model model)
    {
//        //读取数据库中的课程类别数据，供页面使用
//        List<CourseCategory> categoryList = iCourseCategoryService.findAllList();
//        model.addAttribute("categoryList",categoryList);
        return "stuinfo/add";
    }


// 增加或者更新
    @RequestMapping(value = "save")
    @ResponseBody
    public CURDResult save(StuInfo stuInfo){
        CURDResult result = new CURDResult();// {success:1,msg:””}
        if(stuInfo.getId() == null ){
            System.out.println("增加");
            iStuInfoService.save(stuInfo);
        }
        else{
            System.out.println(stuInfo);
            System.out.println("更新");
            iStuInfoService.update(stuInfo);
        }




        return result;
    }

    //更新
    @RequestMapping("edit")
    public String edit(Model model,Integer id){
        System.out.println("修改定单id=" + id);
        StuInfo stuInfo=iStuInfoService.findById(id);
//        //读取数据库中的课程类别数据，供页面使用
//        List<CourseCategory> categoryList = iCourseCategoryService.findAllList();
//        model.addAttribute("categoryList",categoryList);
        model.addAttribute("stuInfo",stuInfo);
        return "stuinfo/edit";
    }

    //删除
    @RequestMapping("delete")
    @ResponseBody
    public CURDResult delete(Integer id){
        CURDResult result = new CURDResult();
        iStuInfoService.deleteById(id);
        return result;
    }
}


